<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'be_escorts';
	protected $_ci;
	protected $_currentUserGroup;
	protected $_currentUserName = '';
	protected $_curentUserZones;
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('be_escort');	
		$this->_currentUserGroup = $this->_ci->ion_auth->get_user()->group;
		$this->_currentUserName = $this->_ci->ion_auth->get_user()->username;
		$this->_curentUserZones = $this->_ci->escort_user_roles_n_zones_entity->getUserRolesNZones($this->_currentUserName);
		
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('https://www.google.com/jsapi?key=ABQIAAAAKNy9jiLs8gd-R75jb5PC5BTMNjZ5kIwLaBRsDZ8wTaOfjEA-AxQ34C53sb_x7AOjh0epJWs2x3G1bQ'))
						->append_metadata(js('http://maps.googleapis.com/maps/api/js?sensor=false'))
						->append_metadata(js('jquery.form.js', 'be_escort'))
						->append_metadata(js('helper.js', 'be_escort'))
						->append_metadata(js('jquery.simplemodal.js', 'be_escort'))
						->append_metadata(js('escort.be.escort.js', 'be_escort'))
						->append_metadata(css('confirm.css', 'be_escort'))
						->append_metadata(css('escort.be.escort.css', 'be_escort'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		//set the base/default where clause
		$base_where = array();
	
		$base_where = $this->input->post('f_site') ? $base_where + array('site' => $this->input->post('f_site')) : $base_where;

		$escortIdList = $this->_ci->user_entity->getEscortIdListByAdmin($this->_currentUserName, $this->_currentUserGroup , $this->_curentUserZones);
		if($escortIdList && count($escortIdList) > 0){
			$total_rows = $this->_ci->escort_entity->countEscortByEscortIdList($escortIdList);
			$pagination = create_pagination('admin/be_escort/index', $total_rows);
			$escorts = $this->_ci->escort_entity->getEscortByEscortIdList($escortIdList, $pagination['limit']);
			if($escorts){
				foreach($escorts as $escort){
					$escort->userInfo = $this->_ci->user_entity->init('id', $escort->user_id);
				}
			}
		}else{
			$pagination = null;
			$escorts = null;
		}
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		
		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('pagination', $pagination)
			->set('escorts', $escorts);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
	
	public function edit($id = 0)
	{
		$id OR redirect('admin/be_escort');
		
		$this->data->publicOptions = array(
				'1' => 	lang('escort_be_escort_published_label'),
				'0' => lang('escort_be_escort_unpublished_label')
			);
		$this->data->lockedOptions = array(
				'0' => 	lang('escort_be_escort_allow_edit_label'),
				'1' => lang('escort_be_escort_donot_allow_edit_label')
		);
		
		$this->data->paymentOptions = array(
				'0' => 	lang('escort_be_escort_enable_payment_label'),
				'1' => lang('escort_be_escort_disable_payment_label')
		);
		
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		
		if(!$escort || !$user){
			redirect('admin/be_escort');
		}
		
		$this->template
		->title($this->module_details['name'], sprintf(lang('escort_be_escort_edit_title'), $user->username))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->append_metadata(js('jquery/jquery.tagsinput.min.js'))
		->append_metadata(js('blog_form.js', 'blog'))
		->append_metadata(css('jquery/jquery.tagsinput.css'))
		->set('escort', $escort)
		->set('user', $user)
		->build('admin/form', $this->data);
	}
	public function set_publish(){
		$escort_id = $this->input->post('escort_id');
		$published = $this->input->post('published');
		$escort = $this->_ci->escort_entity->init('id', $escort_id);
		$escort->setPublished($published);
		$escort->update();
		$result = array(
			'result' => true			
		);
		if($published){
			$result['message'] = lang('escort_be_escort_update_publish_success');
		}else{
			$result['message'] = lang('escort_be_escort_update_unpublish_success');
		}
		echo json_encode($result);
	}
	public function set_payment(){
		$escort_id = $this->input->post('escort_id');
		$payment = $this->input->post('payment_option');
		$escort = $this->_ci->escort_entity->init('id', $escort_id);
		$escort->setPaymentPending($payment);
		$escort->update();
		$result = array(
				'result' => true			
		);
		if($payment){
			$result['message'] = lang('escort_be_escort_disable_payment_success');
		}else{
			$result['message'] = lang('escort_be_escort_enable_payment_success');
		}
		echo json_encode($result);
	}
	public function set_locked(){
		$escort_id = $this->input->post('escort_id');
		$locked = $this->input->post('locked_option');
		if($locked){
			$locked = 1;
		}else{
			$locked = 0;
		}
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $escort_id);
		$lockedEscort->setLockedSex($locked);
		$lockedEscort->setLockedSexualOrientation($locked);
		$lockedEscort->setLockedBirthDate($locked);
		$lockedEscort->setLockedLanguageIds($locked);
		$lockedEscort->setLockedCountry($locked);
		$lockedEscort->setLockedHair($locked);
		$lockedEscort->setLockedEye($locked);
		$lockedEscort->setLockedHeight($locked);
		$lockedEscort->setLockedWeight($locked);
		$lockedEscort->setLockedMeasures($locked);
		$lockedEscort->setLockedSchedule($locked);
		$lockedEscort->setLockedWorkingCalendar($locked);
		$lockedEscort->setLockedWorkingPlaces($locked);
		$lockedEscort->setLockedTravel($locked);
		$lockedEscort->setLockedServeTo($locked);
		$lockedEscort->setLockedServices($locked);
		$lockedEscort->setLockedPaymentMethod($locked);
		$lockedEscort->setLockedPricingTime($locked);
		$lockedEscort->setLockedExtras($locked);
		$lockedEscort->setLockedTransport($locked);
		$lockedEscort->setLockedPhone($locked);
		$lockedEscort->setLockedEmail($locked);
		$lockedEscort->setLockedAddress($locked);
		$lockedEscort->setLockedGeolocation($locked);
		$lockedEscort->setLockedMap($locked);
		$lockedEscort->update();
		$result = array(
			'result' => true			
		);
		if($locked){
			$result['message'] = lang('escort_be_escort_locked_success');
		}else{
			$result['message'] = lang('escort_be_escort_unlocked_success');
		}
		echo json_encode($result);
	}
	
	public function get_state_data(){
		$country_id = $this->input->post('country_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_be_escort_personal_state_label").'</option>';
		if($country_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		
		if($COUNTRY->getCountryCode() == 'ES'){
			$result['spain'] = true;
		}else{
			$result['spain'] = false;
		}
		
		$stateList = getStateData_ioc($country_id);
		
		if($stateList){
			foreach ($stateList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	public function get_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_be_escort_personal_city_label").'</option>';	
		if($state_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		
		if($COUNTRY->getCountryCode() == 'ES'){
			$cityList = getProvinceData_ioc($country_id, $state_id);
			$result['spain'] = true;
			$optionList = '<option value="0">'.lang("escort_be_escort_personal_provice_label").'</option>';	
		}else{
			$result['spain'] = false;
			$cityList = getCityData_ioc($country_id, $state_id, 0);
			$optionList = '<option value="0">'.lang("escort_be_escort_personal_city_label").'</option>';	
		}

		
		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList; 
		echo json_encode($result);
	}
	public function get_spain_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');
		$province_id = $this->input->post('province_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_user_role_all_city_label").'</option>';	
		if($province_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$cityList = getCityData_ioc($country_id, $state_id, $province_id);
		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	public function log_point($id){
		$this->load->model('escort_point_log_m');
		//$total_rows = $this->escort_point_log_m->where('escort_id', $id)->count_all();
		//$pagination = create_pagination('admin/be_escort/statistic/'.$id, $total_rows);
		
		$pointLogList = $this->escort_point_log_m->where('escort_id', $id)->get_all();
		$this->template->set_layout(FALSE);
		$this->template
		//->set('pagination', $pagination)
		->set('pointLogList', $pointLogList)
		->build('admin/statistic', $this->data);
	}
}
